package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.User;
import com.digital.welcome.entity.UserLessPay;
import com.digital.welcome.mapper.UserLessPayMapper;
import com.digital.welcome.mapper.UserMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.UserLessPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author 莲童的雨儿
 */
@Service
public class UserLessPayServiceImpl extends ServiceImpl<UserLessPayMapper, UserLessPay> implements UserLessPayService {
    @Autowired
    private UserLessPayMapper userLessPayMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean selectState(String userName) {
        QueryWrapper<UserLessPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        Integer count = userLessPayMapper.selectCount(queryWrapper);
        return count != 0;
    }

    @Override
    public String userLessPayDo(User user) {
        if (user == null || user.getUserName() == null) {
            return JSON.toJSONString(new Result<>().setCode(0).setMessage("用户名为空"));
        }
        UserLessPay userLessPay = new UserLessPay();
        userLessPay.setUserName(user.getUserName());
        userLessPayMapper.insert(userLessPay);
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("绿色通道办理成功"));
    }

    @Override
    public String selectNumber() {
        Integer count = null;
        QueryWrapper<UserLessPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("user_name");
        count = userLessPayMapper.selectCount(queryWrapper);
        //绿色
        Integer count1 = null;
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.isNotNull("category");
        count1 = userMapper.selectCount(queryWrapper1) - count;
        //总

        HashMap<String, Object> map = new HashMap<>();
        map.put("is", count);
        map.put("no", count1);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("成功").setData(map));

    }
}
